<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use App\Http\Requests\Register;
class RegisterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Register $request)
    {
        //处理前台注册数据

        $data=$request->all();

        $username=$data['username'];
        $password=Hash::make($data['password']);
        $phone=$data['phone'];
        $email=$data['email'];
        //验证验证码是否正确
        if($data['phonecode'] != session('phone'))
        {
            return back()->with('error','手机验证码不正确');

        }

        $id=DB::table('zy_user')->insertGetid(['username'=>$username,'password'=>$password,'phone'=>$phone,'email'=>$email]);

        if($id)
        {
            $res=DB::table('zy_score')->insert(['uid'=>$id,'score'=>'10']);
            DB::table('zy_userdetails')->insert(['uid'=>$id,'sex'=>'男','age'=>'20','nickname'=>'小明',
                                                'photo'=>'/uploads/20170830/20150712231732_t5BPj.thumb.224_0.gif',
                                                'description'=>'这个人很懒哦,并没有留下足迹...','q_count'=>'0','a_count'=>'0','level'=>'0']);
            if($res)
            {

                 return redirect('/home')->with('session','注册成功');
             }else{
                 return redirect('/home/register')->with('errors','注册失败');
             }
        }else{
            return redirect('/home/register')->with('errors','注册失败');

        }
    }



     public function phoneCode(Request $request){

        $phone = $request->input('phone','18516339933');
        $phone1=DB::table('zy_user')->where('phone',$phone)->first();
        if($phone1){
            return "此手机号码已注册";
        }
        $exists=\Redis::exists('STRING:PHONE:CODE:'.$phone);
         if(!empty($exists)){
            return 1;
        }
        $code = rand(1000,9999);
        session(['phone'=>$code]);
        // 18813044687

        $url = 'http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C49867559&password=5ad978e82844d1e9577986a67ee76922&format=json&mobile='.$phone.'&content=您的验证码是：'.$code.'。请不要把验证码泄露给其他人。';

        $ch = curl_init();
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
         $arr = json_decode($res,true);

        // "{"code":2,"msg":"\u63d0\u4ea4\u6210\u529f","smsid":"15047562814675488012"}"
        //array:3 [  "code" => 2  "msg" => "提交成功"  "smsid" => "15047731196511409599"]
        // 将json转化为数组
        if($arr['code']==2){
             \Redis::setex('STRING:PHONE:CODE:'.$phone,60,$code);

            echo $arr['code'];
        }else{

            echo $arr['code'];
        }
    }

    //检测是否输出验证码
    // public function insert(Request $request){
    //     dump($request->all());
    //     echo session('phone');
    // }

    /**
     * 前台注册ajax验证开始
     */
    public function yzuser(Request $request)
    {
        //前台注册页面的ajax验证是否存在用户名
        $username=$request->input('username');
        $data=DB::table('zy_user')->where('username','=',$username)->first();
        if($data)
        {
            //0为存在
            echo 0;
        }else{
            //1为不存在
            echo 1;
        }
    }

    public function yzphone(Request $request)
    {
        $phone=$request->input('phone');
        $data=DB::table('zy_user')->where('phone','=',$phone)->first();
        if($data)
        {
            //0为存在
            echo 0;
        }else{
            //1为不存在
            echo 1;
        }
    }
}
